﻿using System;
using System.Collections.Generic;
using System.Data;
using DataAccess;
using EntitiesTM;

namespace Business
{
    public class BuProject
    {
        public static DataTable selAllProject(bool isEnable, string companyid)
        {
            DaProject daProject = new DaProject();
            return daProject.selAllProject(isEnable, companyid);
        }

        public static DataTable selAllProjectReport(string companyid)
        {
            DaProject daProject = new DaProject();
            return daProject.selAllProjectReport(companyid);
        }

        public static DataTable selProjectRegister(string email, string companyid)
        {
            DaProject daProject = new DaProject();
            return daProject.selProjectRegister(email, companyid);
        }
        public static DataTable selProjectRegister2(string email, string companyid)
        {
            DaProject daProject = new DaProject();
            return daProject.selProjectRegister2(email, companyid);
        }

        public static DataTable selProjectRegisterTmp(string email, int level, int parentID, string companyid)
        {
            DaProject daProject = new DaProject();
            return daProject.selProjectRegisterTmp(email, level, parentID, companyid);
        }

        public static int selTopProjectID(string companyid)
        {
            DaProject daProject = new DaProject();
            DataRow dtTmp = daProject.selTopProjectID(companyid).Rows[0];
            return Int32.Parse(dtTmp["ProjectID"].ToString());
        }

        public static EnProject selProjectByID(int projectID, string companyid)
        {
            DaProject daProject = new DaProject();
            DataRow dtTmp = daProject.selProjectByID(projectID, companyid).Rows[0];
            EnProject project = new EnProject();

            project.clientName = dtTmp["ClientName"].ToString();
            string clientID = dtTmp["ProjectClientID"].ToString();
            if (!clientID.Equals(""))
            {
                project.projectClientID = Int32.Parse(clientID);
            }
            project.projectDescription = dtTmp["ProjectDescription"].ToString();
            project.projectEnable = Boolean.Parse(dtTmp["ProjectEnable"].ToString());
            project.projectEnableMember = Boolean.Parse(dtTmp["ProjectEnableMember"].ToString());
            project.projectEndDate = DateTime.Parse(dtTmp["ProjectEndDate"].ToString());
            project.projectName = dtTmp["ProjectName"].ToString();
            project.projectStartDate = DateTime.Parse(dtTmp["ProjectStartDate"].ToString());
            project.staffID = Int32.Parse(dtTmp["StaffID"].ToString());
            project.staffName = dtTmp["StaffName"].ToString();
            return project;
        }

        public static EnProject selProjectByIDTree(int projectID, string companyid)
        {
            DaProject daProject = new DaProject();
            DataRow dtTmp = daProject.selProjectByIDTree(projectID, companyid).Rows[0];
            EnProject project = new EnProject();

            project.projectDescription = dtTmp["ProjectDescription"].ToString();
            project.projectName = dtTmp["ProjectName"].ToString();
            project.projectParentID = Int32.Parse(dtTmp["ProjectParentID"].ToString());
            project.projectCode = dtTmp["ProjectCode"].ToString();
            project.projectLevel = Int32.Parse(dtTmp["ProjectLevel"].ToString());
            return project;
        }

        public static DataTable selStaffMemberProject(int projectID, string companyid)
        {
            DaProject daProject = new DaProject();
            return daProject.selStaffMemberProject(projectID, companyid);
        }

        public static DataTable selStaffMemberProjectAdd(int projectID, string companyid)
        {
            DaProject daProject = new DaProject();
            return daProject.selStaffMemberProjectAdd(projectID, companyid);
        }

        public static DataTable selTimeUser(int projectID, string companyid)
        {
            DaProject daProject = new DaProject();
            DataTable tmpProject = daProject.selTimeUser(projectID, companyid);
            int numOfRow = tmpProject.Rows.Count;
            if (numOfRow > 0)
            {
                // Duyet cac hang cua datatable
                for (int i = 0; i < numOfRow; i++)
                {
                    int minute = Int32.Parse(tmpProject.Rows[i]["SumMinute"].ToString());
                    if (minute >= 60)
                    {
                        int addHour = minute / 60;
                        minute -= addHour * 60;
                        tmpProject.Rows[i]["SumMinute"] = minute;
                        tmpProject.Rows[i]["SumHour"] = Int32.Parse(tmpProject.Rows[i]["SumHour"].ToString()) + addHour;
                    }
                }
            }
            return tmpProject;
        }

        public static void insProject(EnProject project, string companyid)
        {
            DaProject projectExist = new DaProject();
            projectExist.insProject(project, companyid);
        }

        public static void updProject(EnProject project, string companyid)
        {
            DaProject projectExist = new DaProject();
            projectExist.updProject(project, companyid);
        }
        public static void updProjectTree(EnProject project, string companyid)
        {
            DaProject projectExist = new DaProject();
            projectExist.updProjectTree(project, companyid);
        }
        public static void updProjectTreeChild(int projectID, string companyid)
        {
            DaProject projectExist = new DaProject();
            projectExist.updProjectTreeChild(projectID, companyid);
        }
        public static void delProject(EnProject project, string companyid)
        {
            DaProject projectExist = new DaProject();
            projectExist.delSPProject(project, companyid);
            projectExist.delSAProject(project, companyid);
            projectExist.delActivityProject(project, companyid);
            projectExist.delProject(project, companyid);
        }

        public static void delProjectChild(EnProject project, string companyid)
        {
            DaProject projectExist = new DaProject();
            projectExist.delProjectChild(project, companyid);
        }
        public static void insSP(EnSP sp, string companyid)
        {
            DaProject projectExist = new DaProject();
            projectExist.insSP(sp, companyid);
        }

        public static DataTable selAllTypeTime(string companyid)
        {
            DaProject daProject = new DaProject();
            return daProject.selAllTypeTime(companyid);
        }

        public static void insActivity(EnActivity activity, string companyid)
        {
            DaProject projectExist = new DaProject();
            projectExist.insActivity(activity, companyid);
        }

        public static void insActivity2(EnActivity activity, string companyid)
        {
            DaProject projectExist = new DaProject();
            projectExist.insActivity2(activity, companyid);
        }

        public static void upLevel(EnActivity activity, string companyid)
        {
            DaProject projectExist = new DaProject();
            projectExist.upLevel(activity, companyid);
        }

        public static void downLevel(EnActivity activity, string companyid)
        {
            DaProject projectExist = new DaProject();
            projectExist.downLevel(activity, companyid);
        }

        public static void insProjectMember(EnSP sp, string companyid)
        {
            DaProject projectExist = new DaProject();
            projectExist.insProjectMember(sp, companyid);
        }

        public static void updProjectMember(EnSP sp, string companyid)
        {
            DaProject projectExist = new DaProject();
            projectExist.updProjectMember(sp, companyid);
        }

        public static void delProjectMember(EnSP sp, string companyid)
        {
            DaProject projectExist = new DaProject();
            projectExist.delProjectMember(sp, companyid);
        }

        public static DataTable selProjectExceptID(EnProject project, string companyid)
        {
            DaProject projectExist = new DaProject();
            return projectExist.selProjectExceptID(project, companyid);
        }

        public static DataTable selProjectExceptIDNew(EnProject project, string companyid)
        {
            DaProject projectExist = new DaProject();
            return projectExist.selProjectExceptIDNew(project, companyid);
        }
        public static void copyActivity(EnProject project, int projectID, string companyid)
        {
            DaProject projectExist = new DaProject();
            DataTable listActivity = projectExist.selActivityByProjectExceptID(project, companyid);
            int count = listActivity.Rows.Count;
            DataRow dtTmp;
            EnActivity activity = new EnActivity();

            for (int i = 0; i < count; i++)
            {
                dtTmp = listActivity.Rows[i];
                activity.activityName = dtTmp["ActivityName"].ToString();
                //activity.activityTypeTimeID = Int32.Parse(dtTmp["ActivityTypeTimeID"].ToString());
                activity.activityProjectID = projectID;
                activity.activityEstimateTime = Int32.Parse(dtTmp["ActivityEstimateTime"].ToString());

                BuProject.insActivity(activity, companyid);
            }
        }

        public static DataTable selActivityByProjectExceptID(EnProject project, string companyid)
        {
            DaProject projectExist = new DaProject();
            return projectExist.selActivityByProjectExceptID(project, companyid);
        }

        public static string selProjectByParentID(string companyid)
        {
            DaProject projectExist = new DaProject();
            string result = String.Empty;
            result += "<table style=\"width:100%;\"><tr>";
            result += "<th align=\"center\" style=\"color:#375B91;background-color:#F3F9FF;width:50px;\">Mã</th>";
            result += "<th align=\"center\" style=\"color:#375B91;background-color:#F3F9FF;\">Công việc/sản phẩm</th>";
	        result += "<th align=\"center\" style=\"color:#375B91;background-color:#F3F9FF;\">Ghi chú</th></tr>";
            // Duyet ma cap 1
            DataTable listCode1 = projectExist.selProjectByParentID(0, 0, companyid);
            DataTable listCode2;
            DataTable listCode3;
            string idCode1, idCode2;
            int numOfListCode1 = listCode1.Rows.Count;
            for (int i = 0; i < numOfListCode1; i++)
            {
                idCode1 = listCode1.Rows[i]["ProjectID"].ToString();
                result += "<tr><td align=\"center\">" + listCode1.Rows[i]["ProjectCode"].ToString() + "</td>";
                result += "<td><a href=\"/time/admin/EditProjectTree.aspx?pid=" + idCode1 + "\"><b>" + listCode1.Rows[i]["ProjectName"] + "</b></a></td>";
                result += "<td>" + listCode1.Rows[i]["ProjectDescription"].ToString() + "</td>";
                result += "</tr>";

                // Duyet ma cap 2
                listCode2 = projectExist.selProjectByParentID(Int32.Parse(idCode1), 0, companyid);
                int numOfListCode2 = listCode2.Rows.Count;
                for (int j = 0; j < numOfListCode2; j++)
                {
                    idCode2 = listCode2.Rows[j]["ProjectID"].ToString();
                    result += "<tr><td align=\"center\">" + listCode2.Rows[j]["ProjectCode"].ToString() + "</td>";
                    result += "<td><a href=\"/time/admin/EditProjectTree.aspx?pid=" + idCode2 + "\">|—<b><i>" + listCode2.Rows[j]["ProjectName"] + "</i></b></a></td>";
                    result += "<td>" + listCode2.Rows[j]["ProjectDescription"].ToString() + "</td>";
                    result += "</tr>";

                    // Duyet ma cap 3
                    listCode3 = projectExist.selProjectByParentID(Int32.Parse(idCode2), 0, companyid);
                    int numOfListCode3 = listCode3.Rows.Count;
                    for (int k = 0; k < numOfListCode3; k++)
                    {
                        result += "<tr><td align=\"center\"><a href=\"/time/admin/ViewProject.aspx?pid=" + listCode3.Rows[k]["ProjectID"].ToString() + "\">" + listCode3.Rows[k]["ProjectCode"].ToString() + "</a></td>";
                        result += "<td><a href=\"/time/admin/EditProjectTree.aspx?pid=" + listCode3.Rows[k]["ProjectID"].ToString() + "\">|—|—" + listCode3.Rows[k]["ProjectName"] + "</a></td>";
                        result += "<td>" + listCode3.Rows[k]["ProjectDescription"].ToString() + "</td>";
                        result += "</tr>";
                    }
                }
            }
            result += "</table>";
            return result;
        }

        public static Dictionary<string, string> selProjectByParentIDTree(int exceptID, string companyid)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            DaProject projectExist = new DaProject();

            // Duyet ma cap 1
            DataTable listCode1 = projectExist.selProjectByParentID(0, exceptID,companyid);
            DataTable listCode2;
            string idCode1;
            int numOfListCode1 = listCode1.Rows.Count;
            for (int i = 0; i < numOfListCode1; i++)
            {
                idCode1 = listCode1.Rows[i]["ProjectID"].ToString();
                result.Add("- " + listCode1.Rows[i]["ProjectCode"].ToString()+" : " + listCode1.Rows[i]["ProjectName"].ToString(), idCode1);

                // Duyet ma cap 2
                listCode2 = projectExist.selProjectByParentID(Int32.Parse(idCode1), exceptID,companyid);
                int numOfListCode2 = listCode2.Rows.Count;
                for (int j = 0; j < numOfListCode2; j++)
                {
                    result.Add("- - " +listCode2.Rows[j]["ProjectCode"].ToString()+" : " + listCode2.Rows[j]["ProjectName"].ToString(), listCode2.Rows[j]["ProjectID"].ToString());
                }
            }
            return result;
        }

        //public static Dictionary<string, string> selProjectByParentIDTreeReport(int exceptID, string companyid)
        //{
        //    Dictionary<string, string> result = new Dictionary<string, string>();
        //    DaProject projectExist = new DaProject();

        //    // Duyet ma cap 1
        //    DataTable listCode1 = projectExist.selProjectByParentIDSort(0, exceptID,companyid);
        //    DataTable listCode2, listCode3;
        //    string idCode1, idCode2;
        //    int numOfListCode1 = listCode1.Rows.Count;
        //    for (int i = 0; i < numOfListCode1; i++)
        //    {
        //        idCode1 = listCode1.Rows[i]["ProjectID"].ToString();
        //        result.Add("- " + listCode1.Rows[i]["ProjectName"].ToString(), idCode1);

        //        // Duyet ma cap 2
        //        listCode2 = projectExist.selProjectByParentIDSort(Int32.Parse(idCode1), exceptID,companyid);
        //        int numOfListCode2 = listCode2.Rows.Count;
        //        for (int j = 0; j < numOfListCode2; j++)
        //        {
        //            idCode2 = listCode2.Rows[j]["ProjectID"].ToString();
        //            result.Add("- - " + listCode2.Rows[j]["ProjectName"].ToString(), idCode2);

        //            // Duyet ma cap 3
        //            listCode3 = projectExist.selProjectByParentIDSort(Int32.Parse(idCode2), 0,companyid);
        //            int numOfListCode3 = listCode3.Rows.Count;
        //            for (int k = 0; k < numOfListCode3; k++)
        //            {
        //                string tmp = listCode3.Rows[k]["ProjectName"].ToString()+""+ listCode3.Rows[k]["ProjectID"].ToString();
        //                result.Add("- - - " + listCode3.Rows[k]["ProjectName"].ToString(), listCode3.Rows[k]["ProjectID"].ToString());
        //            }
        //        }
        //    }
        //    return result;
        //}

        //public static Dictionary<string, string> selProjectRegisterTree(string email, string companyid)
        //{
        //    Dictionary<string, string> result = new Dictionary<string, string>();
        //    DaProject projectExist = new DaProject();

        //    // Duyet ma cap 1
        //    DataTable listCode1 = projectExist.selProjectRegisterTree(email, 0,companyid);
        //    DataTable listCode2, listCode3;
        //    string idCode1, idCode2;
        //    int numOfListCode1 = listCode1.Rows.Count;
        //    for (int i = 0; i < numOfListCode1; i++)
        //    {
        //        idCode1 = listCode1.Rows[i]["ProjectID"].ToString();
        //        result.Add("- " + listCode1.Rows[i]["ProjectName"].ToString(), idCode1);

        //        // Duyet ma cap 2
        //        listCode2 = projectExist.selProjectRegisterTree(email, Int32.Parse(idCode1),companyid);
        //        int numOfListCode2 = listCode2.Rows.Count;
        //        for (int j = 0; j < numOfListCode2; j++)
        //        {
        //            idCode2 = listCode2.Rows[j]["ProjectID"].ToString();
        //            result.Add("- - " + listCode2.Rows[j]["ProjectName"].ToString(), idCode2);

        //            // Duyet ma cap 3
        //            listCode3 = projectExist.selProjectRegisterTree(email, Int32.Parse(idCode2),companyid);
        //            int numOfListCode3 = listCode3.Rows.Count;
        //            for (int k = 0; k < numOfListCode3; k++)
        //            {
        //                result.Add("- - - " + listCode3.Rows[k]["ProjectName"].ToString(), listCode3.Rows[k]["ProjectID"].ToString());
        //            }
        //        }
        //    }
        //    return result;
        //}

        public static Dictionary<string, string> selProjectByParentIDTreeReport(int exceptID, string companyid)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            DaProject projectExist = new DaProject();

            // Duyet ma cap 1
            DataTable listCode1 = projectExist.selProjectByParentIDSort(0, exceptID, companyid);
            DataTable listCode2, listCode3;
            string idCode1, idCode2;
            int numOfListCode1 = listCode1.Rows.Count;
            for (int i = 0; i < numOfListCode1; i++)
            {
                idCode1 = listCode1.Rows[i]["ProjectID"].ToString();
                result.Add(idCode1,"- " + listCode1.Rows[i]["ProjectName"].ToString());

                // Duyet ma cap 2
                listCode2 = projectExist.selProjectByParentIDSort(Int32.Parse(idCode1), exceptID, companyid);
                int numOfListCode2 = listCode2.Rows.Count;
                for (int j = 0; j < numOfListCode2; j++)
                {
                    idCode2 = listCode2.Rows[j]["ProjectID"].ToString();
                    result.Add(idCode2,"- - " + listCode2.Rows[j]["ProjectName"].ToString());

                    // Duyet ma cap 3
                    listCode3 = projectExist.selProjectByParentIDSort(Int32.Parse(idCode2), 0, companyid);
                    int numOfListCode3 = listCode3.Rows.Count;
                    for (int k = 0; k < numOfListCode3; k++)
                    {
                        string tmp = listCode3.Rows[k]["ProjectName"].ToString() + "" + listCode3.Rows[k]["ProjectID"].ToString();
                        result.Add(listCode3.Rows[k]["ProjectID"].ToString(),"- - - " + listCode3.Rows[k]["ProjectName"].ToString());
                    }
                }
            }
            return result;
        }

        public static Dictionary<string, string> selProjectRegisterTree(string email, string companyid)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            DaProject projectExist = new DaProject();

            // Duyet ma cap 1
            DataTable listCode1 = projectExist.selProjectRegisterTree(email, 0, companyid);
            DataTable listCode2, listCode3;
            string idCode1, idCode2;
            int numOfListCode1 = listCode1.Rows.Count;
            for (int i = 0; i < numOfListCode1; i++)
            {
                idCode1 = listCode1.Rows[i]["ProjectID"].ToString();
                result.Add(idCode1,"- " + listCode1.Rows[i]["ProjectName"].ToString());

                // Duyet ma cap 2
                listCode2 = projectExist.selProjectRegisterTree(email, Int32.Parse(idCode1), companyid);
                int numOfListCode2 = listCode2.Rows.Count;
                for (int j = 0; j < numOfListCode2; j++)
                {
                    idCode2 = listCode2.Rows[j]["ProjectID"].ToString();
                    result.Add(idCode2,"- - " + listCode2.Rows[j]["ProjectName"].ToString());

                    // Duyet ma cap 3
                    listCode3 = projectExist.selProjectRegisterTree(email, Int32.Parse(idCode2), companyid);
                    int numOfListCode3 = listCode3.Rows.Count;
                    for (int k = 0; k < numOfListCode3; k++)
                    {
                        result.Add(listCode3.Rows[k]["ProjectID"].ToString(),"- - - " + listCode3.Rows[k]["ProjectName"].ToString());
                    }
                }
            }
            return result;
        }
    }
}
